(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('ordersCtrl',ordersCtrl);

    function ordersCtrl(authenticationService, Constants, ordersManager, customerManager, usersManager, $state){

        var _self = this;
        _self.showAll = false;

        this.orders = ordersManager.getOrders();
        this.customers = customerManager.getCustomers();

        this.isSalesman = function(){
            return authenticationService.validateUserRole(Constants.ROLES.SALESMAN);
        };

        this.getOrderCustomerName = function(order){
            return customerManager.getCustomerName(order.customer_id);
        };

        this.getOrderSalesmanUsername = function(order){
            return usersManager.getUsername(order.salesman_id);
        };

        this.getOrderStatus = function(order){
            return ordersManager.getOrdereStatus(order);
        };

        this.filterOrders = function(order){
            if(_self.showAll){
                return true;
            }

            if((order.status === Constants.ORDER_STATUS.OPEN) || (order.status === Constants.ORDER_STATUS.IN_PROGRESS)){
                return true;
            }

            return false;
        };

        this.editOrder = function(order){

            ordersManager.setEditOrder(order);
            if(this.isSalesman()){
                $state.go(Constants.STATES.SALESMAN.ORDER)
            }else{
                $state.go(Constants.STATES.MANAGER.ORDER)
            };
        };

    };

}(angular));